//
//  LoanAgreementController.swift
//  EasyDana
//
//  Created by mac on 2024/10/9.
//

import UIKit
import RxSwift
import RxCocoa
import WebKit
class LoanAgreementController: UIViewController {

    private let bag = DisposeBag()
    
    private let canAgree = PublishSubject<Bool>()
    
    var agree: BehaviorRelay<Bool>?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.configView()
        // Do any additional setup after loading the view.
    }
    
    private func configView() {
        let backView = UIView()
        self.view.addSubview(backView)
        backView.backgroundColor = UIColor.white
        backView.layer.masksToBounds = true
        backView.layer.cornerRadius = 16
        backView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.left.equalToSuperview().offset(18)
            make.height.equalTo(557)
        }
        let titleLabel = UILabel()
        backView.addSubview(titleLabel)
        titleLabel.text = "Loan Agreement".locale
        titleLabel.font = UIFont(name: "Righteous-Regular", size: 20)
        titleLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalToSuperview().offset(16)
        }
        let closeButton = UIButton(type: .system)
        closeButton.setImage(UIImage(named: "login_close")?.withRenderingMode(.alwaysOriginal), for: .normal)
        backView.addSubview(closeButton)
        closeButton.snp.makeConstraints { make in
            make.centerY.equalTo(titleLabel)
            make.right.equalToSuperview().offset(-20)
        }
        closeButton.rx.tap
            .subscribe(onNext: { [weak self] event in
                self?.dismiss(animated: false)
            })
            .disposed(by: self.bag)
        let textBGView = UIView()
        backView.addSubview(textBGView)
        textBGView.layer.masksToBounds = true
        textBGView.layer.cornerRadius = 8
        textBGView.backgroundColor = UIColor.color(with: 242, green: 247, blue: 253)
        textBGView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(titleLabel.snp.bottom).offset(15)
            make.centerX.equalToSuperview()
            make.height.equalTo(419)
        }
        var url = "https://khazana-tradelinks.com/EASY-Agreement.html?" + HTTPManager.default.defaultParam
        url = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? url
        let webView = WKWebView()
        textBGView.addSubview(webView)
        webView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        webView.load(URLRequest(url: URL(string: url)!))
        
        let agreeButton = UIButton(type: .custom)
        agreeButton.setBackgroundImage(UIImage(named: "loan_agree_normal")?.withRenderingMode(.alwaysOriginal), for: .normal)
        agreeButton.setBackgroundImage(UIImage(named: "loan_agree_disable")?.withRenderingMode(.alwaysOriginal), for: .disabled)
        agreeButton.titleLabel?.font = UIFont(name: "Righteous-Regular", size: 20)
        agreeButton.setTitle("Agree and continue".locale, for: .normal)
        agreeButton.setTitleColor(UIColor.white, for: .normal)
        backView.addSubview(agreeButton)
        agreeButton.snp.makeConstraints { make in
            make.top.equalTo(textBGView.snp.bottom).offset(15)
            make.centerX.equalToSuperview()
        }
        agreeButton.isEnabled = false
        agreeButton.setTitle("Agree and continue".locale + " (\(10))", for: .disabled)
        Observable<Int>
            .interval(.seconds(1), scheduler: MainScheduler.instance)
            .take(10)
            .subscribe { time in
                agreeButton.isEnabled = false
                agreeButton.setTitle("Agree and continue".locale + " (\(9 - time))", for: .disabled)
            } onCompleted: {
                agreeButton.isEnabled = true
            }
            .disposed(by: self.bag)
        agreeButton.rx.tap
            .subscribe(onNext: { event in
                self.agree?.accept(true)
                self.dismiss(animated: false)
            })
            .disposed(by: self.bag)
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
